﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class SlotSniper : SlotWeapon
    {
        protected const int NB_PER_WAVE = 20;
        public SlotSniper(WorldProperties worldProperties, SlotKind kind, Tower ownerTower)
            : base(worldProperties, kind, ownerTower,false)
        {
            rangeDisplayColor = Color.Black;
            RangedFlightSpeed = 1500;
        }
        public override GameObject getTarget()
        {
            GameObject target = null;
            // target enemies
            foreach (EnemyUnit possible in worldProperties.Enemies)
            {
                //get the angle with the enemy
                Vector2 direction = possible.Position - Position;
                direction.Normalize();
                direction.Y = -direction.Y;
                float angle_direction;
                if (direction.Y >= 0)
                {
                    angle_direction = (float)Math.Acos(direction.X) % MathHelper.TwoPi;
                }
                else
                {
                    angle_direction = -(float)Math.Acos(direction.X) % MathHelper.TwoPi;
                }
                //make sure it's between 0 and 2 PI
                if (angle_direction < 0)
                {
                    angle_direction += MathHelper.TwoPi;
                }

                float slotRotation = orientation + ownerTower.Rotation;

                //get the minimum angle
                float min_angle = ((-slotRotation - angle)) % MathHelper.TwoPi;
                //make sure it's between 0 and 2 PI
                if (min_angle < 0)
                {
                    min_angle += MathHelper.TwoPi;
                }

                //get the maximum angle
                float max_angle = ((-slotRotation + angle)) % MathHelper.TwoPi;
                //get sure it's between 0 and 2 PI
                if (max_angle < 0)
                {
                    max_angle += MathHelper.TwoPi;
                }

                bool isTarget = false;
                if (max_angle < min_angle)
                {
                    if (angle_direction < max_angle || angle_direction > min_angle && angle_direction > max_angle)
                    {
                        isTarget = true;
                    }
                }
                else
                {
                    if (angle_direction < max_angle && angle_direction > min_angle)
                    {
                        isTarget = true;
                    }
                }

                if (isTarget)
                {
                    if (target==null || ((EnemyUnit)possible).Kind < ((EnemyUnit)target).Kind)
                    {
                        target = possible;
                    }
                }
            }

            // check for other targets here
            return target;
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Sprite = Game.Content.Load<Texture2D>("Sprites/cannon_1");
        }
        
    }
}

